package org.example.service.impl.en;

import org.example.mapper.DepositTbMapper;
import org.example.mapper.en.DepositTbEnMapper;
import org.example.pojo.en.DepositTbPageEn;
import org.example.pojo.en.UsersTbPageEn;
import org.example.service.api.en.DepositEnServiceInterface;
import org.example.util.JSONStats;
import org.example.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户页面相关操作
 */
@Service
public class DepositEnServiceInterfaceImpl implements DepositEnServiceInterface {

    @Autowired
    DepositTbEnMapper depositTbEnMapper;
    @Override
    public PageList queryDepositInfoById(Integer depositId, Integer page, Integer pageSize) {
        PageList pageList= new PageList();
        int start;
        //对页面进行判断
        if(page==0){
            start=0;
        }else {
            start=(page-1)*pageSize;
        }

        List<DepositTbPageEn> depositTbPageEns =depositTbEnMapper.queryDepositInfoByDepositId(depositId,start,pageSize);
        if (depositTbPageEns.size() > 0) {
            //定义totalPageCount
            int totalPageCount;
            //查询成功
            //取里面的总数
            int totalCount = depositTbPageEns.get(0).getTotalCount();
            //封装返回结果
            //对总页数进行处理
            if(totalCount%pageSize==0){
                totalPageCount=totalCount/pageSize;
            }else {
                totalPageCount=totalCount/pageSize + 1;
            }
            System.out.println(totalPageCount);
            pageList.setPage(page);
            pageList.setPageList(depositTbPageEns);
            pageList.setPageTotal(totalPageCount);
            pageList.setPageSize(pageSize);
            pageList.setMessage(JSONStats.QUERY_SUCCESS.getMessage());
            pageList.setStatusCode(JSONStats.QUERY_SUCCESS.getStatusCode());

        }else {
            //查询失败
            pageList.setMessage(JSONStats.QUERY_ERROR.getMessage());
            pageList.setStatusCode(JSONStats.QUERY_ERROR.getStatusCode());

        }

        return pageList;
    }

    /**
     * 通过用户信息查询充值记录
     * @param userId 用户id
     * @param page 用户页
     * @param pageSize 页面大小
     * @return
     */
    @Override
    public PageList queryDepositInfoByUserId(Integer userId, Integer page, Integer pageSize) {
        PageList pageList = new PageList();

        //起点
        int start;
        //对页面进行处理
        if(page==1){
            start=0;
        }else{
            start= (page-1) * pageSize;
        }
        //调用Dao 获取userID查询的数据集合
        List<DepositTbPageEn> depositTbPageEnList= depositTbEnMapper.queryDepositInfoByUserId(userId,start,pageSize);
        if(depositTbPageEnList.size()>0){
            int totalCount = depositTbPageEnList.get(0).getTotalCount();
            //获取页面总数
            int totalPageCount;
            //获取总数
            if(totalCount%pageSize==0){
                totalPageCount= totalCount/pageSize;
            }else {
                totalPageCount= totalCount/pageSize +1;
            }
            //设置数据
            pageList.setPageTotal(totalPageCount);
            //设置页面大小
            pageList.setPageSize(pageSize);
            //设置当前页面
            pageList.setPage(page);
            //设置数据
            pageList.setPageList(depositTbPageEnList);
            //设置响应数据
            pageList.setMessage(JSONStats.QUERY_SUCCESS.getMessage());
            pageList.setStatusCode(JSONStats.QUERY_SUCCESS.getStatusCode());

        }else {
            //失败
            //设置响应数据
            pageList.setMessage(JSONStats.QUERY_ERROR.getMessage());
            pageList.setStatusCode(JSONStats.QUERY_ERROR.getStatusCode());

        }
        return pageList;
    }


    @Override
    public PageList queryDepositInfoByDormitoryId(Integer page, Integer pageSize, Integer dormitoryId) {
        PageList pageList= new PageList<>();

        int start;
        //对页面进行处理
        if(page==1){
            start=0;
        }else{
            start= (page-1) * pageSize;
        }


        List<DepositTbPageEn> depositTbPageEnList=depositTbEnMapper.queryDepositInfoByDormitoryId(dormitoryId,start,pageSize);


        //如果集合不为空
        if(depositTbPageEnList.size()>0){
            //取总数
            int  counts= depositTbPageEnList.get(0).getTotalCount();

            //对总数进行处理
            int pageTotal;
            if(counts%page==0){
                pageTotal=counts/page;
            }else {
                pageTotal=counts/page + 1;
            }
            //设置页数
            pageList.setPageTotal(pageTotal);
            //设置页面大小
            pageList.setPageSize(pageSize);
            pageList.setPage(page);
            pageList.setPageList(depositTbPageEnList);
            //设置响应结果
            pageList.setStatusCode(JSONStats.QUERY_SUCCESS.getStatusCode());
            pageList.setMessage(JSONStats.QUERY_SUCCESS.getMessage());

            //返回响应结果
        }else {
            //失败
            //设置响应结果
            pageList.setStatusCode(JSONStats.QUERY_ERROR.getStatusCode());
            pageList.setMessage(JSONStats.QUERY_ERROR.getMessage());

        }
        return pageList;

    }

    //管理员like查询
    @Override
    public PageList queryUserInfoByLike(String condition, Integer page, Integer pageSize) {
        //创建PageList对象
        PageList pageList = new PageList();

        int start;
        //计算起始位置

        start= (page-1) * pageSize;

        //调用dao
        List<UsersTbPageEn> pageEnList= depositTbEnMapper.getUserInfoByLike(condition,start,pageSize);

        if(pageEnList.size()>0){
            //说明有数据
            //页面总数
            int totalPageCount;
            int totalCount = pageEnList.get(0).getTotalCount();
            System.out.println(totalCount);
            if(totalCount%pageSize==0){
                totalPageCount=totalCount/pageSize;
            }else {
                totalPageCount=totalCount/pageSize +1;
            }
            //设置下页面数据
            pageList.setPageList(pageEnList);
            //设置当前是第几页面
            pageList.setPage(page);
            //设置当前页面的大小
            pageList.setPageSize(pageSize);
            //设置总页面数
            pageList.setPageTotal(totalPageCount);
            //设置响应状态码
            pageList.setStatusCode(JSONStats.QUERY_SUCCESS.getStatusCode());
            //设置响应信息
            pageList.setMessage(JSONStats.QUERY_SUCCESS.getMessage());
        }else {
            //没有数据查询失败
            pageList.setStatusCode(JSONStats.QUERY_ERROR.getStatusCode());
            pageList.setMessage(JSONStats.QUERY_ERROR.getMessage());
        }


        return pageList;
    }
}
